using System;
using System.Collections;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Common;
using Sedna.Core.Controls.Support;
using Sedna.Core.Controls.Support.Javascript;
using Sedna.Util;
using Iesi.Collections;

namespace Sedna.Core.Controls.Actions
{
    /// <summary>
    /// Extracts Marked data whic is not located on current page into client var
    /// </summary>
    public class ExtractMarkedDataToClientVarAction: AbstractAction
    {
        private String clientVariable;
        private String propertyToExtract;
        private String markedDataPath;
        private String pageDataPath;

        #region Properties

        public string MarkedDataPath
        {
            set { markedDataPath = value; }
        }

        public string PageDataPath
        {
            set { pageDataPath = value; }
        }

        public string ClientVariable
        {
            set { clientVariable = value; }
        }

        public string PropertyToExtract
        {
            set { propertyToExtract = value; }
        }

        #endregion
        
        public override IActionResult Execute(IExecutionContext executionContext)
        {
            ISet markedData = new HashedSet(scopePathParser.Parse(markedDataPath).Value == null ?
                new HashedSet() : (ISet)scopePathParser.Parse(markedDataPath).Value);

            IList pageData = scopePathParser.Parse(pageDataPath).Value == null
                                 ?
                             new ArrayList()
                                 : (IList)scopePathParser.Parse(pageDataPath).Value;

            ArrayList list = new ArrayList();
            foreach (Object o in markedData)
            {
                if (!pageData.Contains(o))
                    list.Add(Utility.GetPropertyValue(o, propertyToExtract).ToString());
            }
            
            
//            JavascriptUtil.VariableDeclaration(clientVariable, JavascriptUtil.ToScriptObject(list));
            
            JavascriptController.Current.DeclareArray(clientVariable, list.ToArray());
            
            return CreateResult(successStatus);
        }
    }
}
